package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.orderVo.CourseWebVoOrder;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.frontvo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontvo.CourseWebVo;
import com.atguigu.eduservice.service.ChapterService;
import com.atguigu.eduservice.service.CourseService;
import com.atguigu.utils.JwtUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @create: 2022/10/9
 * @Description:
 * @FileName: TeacherFrontController
 * @自定义内容：
 */
@RestController
@RequestMapping("/eduservice/courseFront")
public class CourseFrontController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private OrderClient orderClient;

    //课程条件查询带分页
    @PostMapping("getCourseFrontList/{currentPage}/{limit}")
    public R getCourseFrontList(@PathVariable Long currentPage, @PathVariable Long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {

        Page<EduCourse> coursePage = new Page<>(currentPage, limit);
        Map<String, Object> map = courseService.getCourseFrontList(coursePage,courseFrontVo);

        //返回所有数据
        return R.ok().data(map);
    }

    //课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);

        //根据课程id查询章节和小节
        List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
        System.out.println(chapterVideoList);

        //根据课程id和用户id查询订单状态

        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isBlank(memberId)){
            return R.error().code(28004).message("请先登录");
        }
        System.out.println(memberId);
        Boolean isBuyCourse = orderClient.getStatusByCourseId(courseId, memberId);

        return R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy", isBuyCourse);
    }

    @CacheEvict(value = "course",allEntries = true)
    //根据课程id查询课程信息
    @GetMapping("getCourseInfoOrder/{courseId}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String courseId){
        CourseWebVo baseCourseInfo = courseService.getBaseCourseInfo(courseId);

        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(baseCourseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }


}
